package com.hp.team.bookstore.dbutil.init;

import java.util.Calendar;

import com.hp.team.bookstore.dbutil.dao.DAOFactory;
import com.hp.team.bookstore.dbutil.dao.HibernateUtil;
import com.hp.team.bookstore.dbutil.dao.profile.CustomerHibernateDAO;
import com.hp.team.bookstore.model.profile.Address;
import com.hp.team.bookstore.model.profile.Customer;
import com.hp.team.bookstore.model.profile.Gender;
import com.hp.team.bookstore.model.profile.Telephone;

public class CustomerInitializer {

	private static CustomerHibernateDAO customerDAO;

	static {
		DAOFactory daoFactory = new DAOFactory();
		customerDAO = daoFactory.getCustomerDAO();
	}

	public static void init() {

		customerDAO.setSession(HibernateUtil.getSessionFactory().getCurrentSession());
		HibernateUtil.beginWriteTransaction();

		for (int i = 0; i < 12; i++) {
			Customer customer = createCustomer(i);
			customerDAO.makePersistent(customer);
		}

		HibernateUtil.commitTransaction();
	}

	public static Customer createCustomer(long id) {
		Address home = new Address();
		home.setBuildingNumber("Home - Building Number #" + id);
		home.setStreet("Home - Street #" + id);
		home.setState("Home - State #" + id);
		home.setZipCode("Home - Zip #" + id);
		home.setCity("Home - City #" + id);
		home.setCountry("Home - Country #" + id);

		Address office = new Address();
		office.setBuildingNumber("Office - Building Number#" + id);
		office.setStreet("Office - Street #" + id);
		office.setState("Office - State #" + id);
		office.setZipCode("Office - Zip #" + id);
		office.setCity("Office - City #" + id);
		office.setCountry("Office - Country #" + id);

		Telephone homeTele = new Telephone();
		homeTele.setCountryCode("Home Tele - Country Code#" + id);
		homeTele.setNumber("Home Tele - Number #" + id);

		Telephone officeTele = new Telephone();
		officeTele.setCountryCode("Office Tele - Country Code#" + id);
		officeTele.setNumber("Office Tele - Number #" + id);

		Calendar date = Calendar.getInstance();
		date.set(2010, 5, 1);

		Customer customer = new Customer();
		customer.setFirstName("first #" + id);
		customer.setLastName("last #" + id);
		customer.setAddress(home);
		customer.setTelephone(homeTele);
		customer.setEmail("customer" + id + "@hp.com");
		customer.setGender(id % 2 == 0 ? Gender.MALE : Gender.FEMALE);

		return customer;
	}

	public static void main(String[] args) {
		CustomerInitializer.init();
	}

}
